﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace game
{
    public class ExplosionPSystem : BillboardParticleSystem
    {
        public class Factory : FactoryPool<ExplosionPSystem>
        {
            public Factory(Scene scene, int initialSize) : base(scene, initialSize){}

            public override ExplosionPSystem CreateObject()
            {
                ExplosionPSystem ep = new ExplosionPSystem(scene);
                ep.Initialize();
                return ep;
            }
        }

        public override bool IsAlive
        {
            get
            {
                return elapsedSystemTimeMs < MaxLifeTimeMs;
            }
        }

        protected Vector3 nextBangPos = new Vector3(0, 0, 0);
        protected float nextBangRadius = 10;

        public ExplosionPSystem(Scene scene)
            : base(scene, Vector3.Zero)
        {
        }

        protected override void InitParameters()
        {
            textureName = "Textures\\plasma";
            effectName = "Effects\\BillboardParticle";
            effectTechName = "DynamicBillboardParticleAdditiveBlending";

            Acceleration = -0.001f;
            maxParticles = 12;

            emitter = null;

            MinInitialSpeed = 0.0f;
            MaxInitialSpeed = 2.0f;
            MinInitialSize = 3000;
            MaxInitialSize = 6000;
            MinLifeTimeMs = 800;
            MaxLifeTimeMs = 1500;

            color = Color.White;
        }

        

        public void Bang(Texture texture, Vector3 position, float particleSize,
            float radius, float particleSpeed, int particlesToUse)
        {
            this.texture = texture;
            nextBangPos = position;

            MaxInitialSize = particleSize;
            MinInitialSize = particleSize * 0.5f;

            nextBangRadius = radius;

            MaxInitialSpeed = particleSpeed;
            MinInitialSpeed = particleSpeed * 0.5f;
            
            SetParticles(particlesToUse);

            //bSphere = new BoundingSphere(nextBangPos, nextBangRadius + MaxInitialSize);
        }

        protected override void InitParticle(int index, Vector3 position, float initialTimeMsOffset)
        {
            base.InitParticle(index, Vector3.Zero, initialTimeMsOffset);

            vertices[index].InitialPosition = nextBangPos + new Vector3(
                RandomHelper.RandomBetween(-nextBangRadius, nextBangRadius),
                RandomHelper.RandomBetween(-nextBangRadius, nextBangRadius),
                RandomHelper.RandomBetween(-nextBangRadius, nextBangRadius)
                );
            vertices[index + 1].InitialPosition = vertices[index].InitialPosition;
            vertices[index + 2].InitialPosition = vertices[index].InitialPosition;
            vertices[index + 3].InitialPosition = vertices[index].InitialPosition;

            float speed = RandomHelper.RandomBetween(MinInitialSpeed, MaxInitialSpeed);
            vertices[index].Velocity = RandomHelper.RandomVector() * speed;
            vertices[index + 1].Velocity = vertices[index].Velocity;
            vertices[index + 2].Velocity = vertices[index].Velocity;
            vertices[index + 3].Velocity = vertices[index].Velocity;
        }
    }
}
